package com.activiti.z_six.service.impl;

import com.activiti.z_six.controller.controllerParams.UserParams;
import com.activiti.z_six.entity.UserInfo;
import com.activiti.z_six.entity.orgmanagement.*;
import com.activiti.z_six.entity.ruoyiorg.SysRole;
import com.activiti.z_six.mapper.UserInfoMapper;
import com.activiti.z_six.mapper.orgmanagementMapper.*;
import com.activiti.z_six.service.OrgManagementService;
import com.activiti.z_six.util.AjaxResponse;
import com.activiti.z_six.util.SystemConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class OrgManagementServiceImpl implements OrgManagementService {
    @Autowired
    private CompanyEntityMapper companyEntityMapper;
    @Autowired
    private DepartmentEntityMapper departmentEntityMapper;
    @Autowired
    private UserEntityMapper userEntityMapper;
    @Autowired
    private PasswordEncoder passwordEncoder;
    @Autowired
    private RolesEntityMapper rolesEntityMapper;
    @Autowired
    private PositionEntityMapper positionEntityMapper;
    @Autowired
    UserInfoMapper mapper;
    /**
     * 获取公司列表
     * @return
     */
    @Override
    public AjaxResponse getCompanyList(){
        try{
            List<CompanyEntity> companyEntityList=getCompanyEntityList();

            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.SUCCESS.getCode(),
                    SystemConfig.ResponseCode.SUCCESS.getDesc(), companyEntityList);
        }
        catch (Exception ex){
            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.ERROR.getCode(),
                    "查询失败", ex.toString());
        }
    }
    private List<CompanyEntity> getCompanyEntityList(){
        List<CompanyEntity> companyEntityList= companyEntityMapper.getCompanyList();
        //2、组装树形结构
        List<CompanyEntity> newList = companyEntityList.stream()
                .filter(t -> t.getParentid() == 0)
                .map((menu) -> {
                    menu.setChildren(this.getCompanyChildren(menu,companyEntityList));
                    return menu;
                })
                .collect(Collectors.toList());
        return newList;
    }
    /**
     * 增加公司
     * @param name
     * @param parentid
     * @return
     */
    @Override
    public AjaxResponse addCompany(String name,Integer parentid){
        try{
            CompanyEntity companyEntity=new CompanyEntity();
            companyEntity.setName(name);
            companyEntity.setParentid(parentid);
            companyEntityMapper.addCompany(companyEntity);

            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.SUCCESS.getCode(),
                    SystemConfig.ResponseCode.SUCCESS.getDesc(), "添加成功。");
        }catch (Exception ex){
            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.ERROR.getCode(),
                    "增加失败", ex.toString());
        }
    }
    /**
     * 修改公司信息
     * @param id
     * @param name
     * @param parentid
     * @return
     */
    @Override
    public AjaxResponse updateCompany(Integer id,String name,Integer parentid){
        try{
            CompanyEntity companyEntity=new CompanyEntity();
            companyEntity.setId(id);
            companyEntity.setName(name);
            companyEntity.setParentid(parentid);
            companyEntityMapper.updateCompany(companyEntity);

            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.SUCCESS.getCode(),
                    SystemConfig.ResponseCode.SUCCESS.getDesc(), "修改成功。");
        }catch (Exception ex){
            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.ERROR.getCode(),
                    "修改失败", ex.toString());
        }
    }
    /**
     * 删除公司信息
     * @param id
     * @param name
     * @param parentid
     * @return
     */
    @Override
    public AjaxResponse deleteCompany(Integer id,String name,Integer parentid){
        try{
            CompanyEntity companyEntity=new CompanyEntity();
            companyEntity.setId(id);
            companyEntity.setName(name);
            companyEntity.setParentid(parentid);
            companyEntityMapper.deleteCompany(companyEntity);

            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.SUCCESS.getCode(),
                    SystemConfig.ResponseCode.SUCCESS.getDesc(), "删除成功。");
        }catch (Exception ex){
            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.ERROR.getCode(),
                    "删除失败", ex.toString());
        }
    }
    /**
     * 递归查找当前公司的子公司
     * @param root 单个对象
     * @param all 所有的集合
     * @return 排序后的子类
     */
    private List<CompanyEntity> getCompanyChildren(CompanyEntity root,List<CompanyEntity> all){
        List<CompanyEntity> childrenList = all.stream()
                .filter(t -> t.getParentid() == root.getId())
                .map(g -> {
                    //找子菜单
                    g.setChildren(getCompanyChildren(g,all));
                    return g;
                })
                //菜单排序
                .collect(Collectors.toList());

        return childrenList;
    }
    /**
     * 获取部门列表
     * @return
     */
    @Override
    public AjaxResponse getDepartmentList(String name,String companyid,Integer page,Integer pageSize){
        try{
            HashMap<String,Object> hashMap=new HashMap<>();
            List<DepartmentEntity> departmentEntities=departmentEntityMapper.getDepartmentList(name,companyid);
            if(pageSize==0){
                hashMap.put("total",departmentEntities.size());
                hashMap.put("list",departmentEntities);
            }
            else {
                Integer indexStart=(page-1)*pageSize;
                List<DepartmentEntity> departmentList = departmentEntityMapper.getDepartmentPage(name, companyid, indexStart, pageSize);
                hashMap.put("total",departmentEntities.size());
                hashMap.put("list",departmentList);
            }
            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.SUCCESS.getCode(),
                    SystemConfig.ResponseCode.SUCCESS.getDesc(), hashMap);
        }
        catch (Exception ex){
            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.ERROR.getCode(),
                    "查询失败", ex.toString());
        }
    }

    /**
     * 获取公司下的部门
     * @param companyId
     * @return
     */
    @Override
    public AjaxResponse getDeptByCompany(Integer companyId){
        try{
            List<DepartmentEntity> departmentList=getDepartmentEntityListByCompany(companyId);

            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.SUCCESS.getCode(),
                    SystemConfig.ResponseCode.SUCCESS.getDesc(), departmentList);
        }
        catch (Exception ex){
            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.ERROR.getCode(),
                    "查询失败", ex.toString());
        }
    }
    private List<DepartmentEntity> getDepartmentEntityListByCompany(Integer companyId){
        List<DepartmentEntity> departmentList= departmentEntityMapper.getDeptByCompany(companyId);
        //2、组装树形结构
        List<DepartmentEntity> newList = departmentList.stream()
                .filter(t -> t.getParentid() == 0)
                .map((menu) -> {
                    menu.setChildren(this.getDeptChildren(menu,departmentList));
                    return menu;
                })
                .collect(Collectors.toList());
        return newList;
    }
    private List<DepartmentEntity> getDepartmentEntityList(){
        List<DepartmentEntity> departmentList= departmentEntityMapper.getDepartmentList("","all");
        //2、组装树形结构
        List<DepartmentEntity> newList = departmentList.stream()
                .filter(t -> t.getParentid() == 0)
                .map((menu) -> {
                    menu.setChildren(this.getDeptChildren(menu,departmentList));
                    return menu;
                })
                .collect(Collectors.toList());
        return newList;
    }
    /**
     * 增加部门
     * @param name
     * @param parentid
     * @param manager
     * @param leader
     * @param companyid
     * @return
     */
    @Override
    public AjaxResponse addDepartment(String name,Integer parentid,String manager,
                                      String leader,Integer companyid){
        try{
            DepartmentEntity departmentEntity=new DepartmentEntity();
            departmentEntity.setName(name);
            departmentEntity.setParentid(parentid);
            departmentEntity.setManager(manager);
            departmentEntity.setLeader(leader);
            departmentEntity.setCompanyid(companyid);

            departmentEntityMapper.addDepartment(departmentEntity);
            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.SUCCESS.getCode(),
                    SystemConfig.ResponseCode.SUCCESS.getDesc(), "添加成功。");
        }
        catch (Exception ex){
            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.ERROR.getCode(),
                    "增加失败", ex.toString());
        }
    }
    /**
     * 修改部门
     * @param id
     * @param name
     * @param parentid
     * @param manager
     * @param leader
     * @param companyid
     * @return
     */
    @Override
    public AjaxResponse updateDepartment(Integer id,String name,Integer parentid,
                                         String manager,String leader,Integer companyid){
        try{
            DepartmentEntity departmentEntity=new DepartmentEntity();
            departmentEntity.setId(id);
            departmentEntity.setName(name);
            departmentEntity.setParentid(parentid);
            departmentEntity.setManager(manager);
            departmentEntity.setLeader(leader);
            departmentEntity.setCompanyid(companyid);

            departmentEntityMapper.updateDepartment(departmentEntity);
            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.SUCCESS.getCode(),
                    SystemConfig.ResponseCode.SUCCESS.getDesc(), "修改成功。");
        }
        catch (Exception ex){
            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.ERROR.getCode(),
                    "修改失败", ex.toString());
        }
    }
    /**
     * 删除部门
     * @param id
     * @return
     */
    @Override
    public AjaxResponse deleteDepartment(Integer id){
        try{
            DepartmentEntity departmentEntity=new DepartmentEntity();
            departmentEntity.setId(id);

            departmentEntityMapper.deleteDepartment(departmentEntity);
            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.SUCCESS.getCode(),
                    SystemConfig.ResponseCode.SUCCESS.getDesc(), "删除成功。");
        }
        catch (Exception ex){
            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.ERROR.getCode(),
                    "删除失败", ex.toString());
        }
    }
    /**
     * 设置部门负责人
     * @param id
     * @param manager
     * @return
     */
    @Override
    public AjaxResponse setDepartmentManager(Integer id,String manager){
        try{
            DepartmentEntity departmentEntity=new DepartmentEntity();
            departmentEntity.setId(id);
            departmentEntity.setManager(manager);

            departmentEntityMapper.setDepartmentManager(departmentEntity);
            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.SUCCESS.getCode(),
                    SystemConfig.ResponseCode.SUCCESS.getDesc(), "设置成功。");
        }
        catch (Exception ex){
            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.ERROR.getCode(),
                    "设置失败", ex.toString());
        }
    }
    /**
     * 设置部门分管领导
     * @param id
     * @param leader
     * @return
     */
    @Override
    public AjaxResponse setDepartmentLeader(Integer id,String leader){
        try{
            DepartmentEntity departmentEntity=new DepartmentEntity();
            departmentEntity.setId(id);
            departmentEntity.setLeader(leader);

            departmentEntityMapper.setDepartmentLeader(departmentEntity);
            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.SUCCESS.getCode(),
                    SystemConfig.ResponseCode.SUCCESS.getDesc(), "设置成功。");
        }
        catch (Exception ex){
            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.ERROR.getCode(),
                    "设置失败", ex.toString());
        }
    }
    /**
     * 设置所属公司
     * @param id
     * @param companyid
     * @return
     */
    @Override
    public AjaxResponse updateDeptCompany(Integer id, Integer companyid){
        try{
            DepartmentEntity departmentEntity=new DepartmentEntity();
            departmentEntity.setId(id);
            departmentEntity.setCompanyid(companyid);

            departmentEntityMapper.updateDeptCompany(departmentEntity);
            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.SUCCESS.getCode(),
                    SystemConfig.ResponseCode.SUCCESS.getDesc(), "设置成功。");
        }
        catch (Exception ex){
            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.ERROR.getCode(),
                    "设置失败", ex.toString());
        }
    }
    /**
     * 获取组织结构数据
     * @return
     */
    @Override
    public AjaxResponse getOrgData(){
        try{

            List<DepartmentEntity> departmentList=getDepartmentEntityList();
            List<UserEntity> userEntityList=userEntityMapper.getUserEntityList("","","all");
            List<RolesEntity> rolesEntities=rolesEntityMapper.rolesList("");
            List<PositionEntity> positionEntities=positionEntityMapper.positionList("");
            List<CompanyEntity> companyEntities=getCompanyEntityList();
            HashMap<String,Object> hashMap=new HashMap<>();

            hashMap.put("departList",departmentList);
            hashMap.put("userList",userEntityList);
            hashMap.put("rolesList",rolesEntities);
            hashMap.put("positionList",positionEntities);
            hashMap.put("companyList",companyEntities);

            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.SUCCESS.getCode(),
                    SystemConfig.ResponseCode.SUCCESS.getDesc(), hashMap);
        }
        catch (Exception ex){
            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.ERROR.getCode(),
                    "设置失败", ex.toString());
        }
    }
    /**
     * 递归查找当前部门的子部门
     * @param root 单个对象
     * @param all 所有的集合
     * @return 排序后的子类
     */
    private List<DepartmentEntity> getDeptChildren(DepartmentEntity root,List<DepartmentEntity> all){
        List<DepartmentEntity> childrenList = all.stream()
                .filter(t -> t.getParentid() == root.getId())
                .map(g -> {
                    //找子菜单
                    g.setChildren(getDeptChildren(g,all));
                    return g;
                })
                //菜单排序
                .collect(Collectors.toList());

        return childrenList;
    }

    /**
     * 获取岗位列表
     * @return
     */
    @Override
    public List<PositionEntity> positionList(){
        return positionEntityMapper.positionList("");
    }

    /**
     * 获取岗位列表
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public HashMap<String,Object> positionListPage(String name,Integer page,Integer pageSize){
        HashMap<String,Object> hashMap=new HashMap<>();
        List<PositionEntity> positionEntities=positionEntityMapper.positionList(name);
        if(pageSize==0){
            hashMap.put("list",positionEntities);
            hashMap.put("total",positionEntities.size());
        }
        else{
            Integer indexStart=(page-1)*pageSize;
            List<PositionEntity> positionEntityList=positionEntityMapper.positionListPage(name,indexStart,pageSize);
            hashMap.put("list",positionEntityList);
            hashMap.put("total",positionEntities.size());
        }
        return hashMap;
    }

    /**
     * 获取用户的岗位
     * @param username
     * @return
     */
    @Override
    public List<PositionEntity> positionListByUser(String username){
        return positionEntityMapper.positionListByUser(username);
    }
    /**
     * 增加岗位
     * @param name
     */
    @Override
    public AjaxResponse addPosition(String name){
        PositionEntity positionEntity=new PositionEntity();
        positionEntity.setId(UUID.randomUUID().toString());
        positionEntity.setName(name);
        try {
            positionEntityMapper.addPosition(positionEntity);
            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.SUCCESS.getCode(),
                    SystemConfig.ResponseCode.SUCCESS.getDesc(), "添加成功");
        }
        catch (Exception ex){
            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.ERROR.getCode(),
                    SystemConfig.ResponseCode.ERROR.getDesc(), ex.toString());
        }
    }

    /**
     * 更新岗位信息
     * @param id
     * @param name
     * @return
     */
    @Override
    public AjaxResponse updatePosition(String id,String name){
        PositionEntity positionEntity=new PositionEntity();
        positionEntity.setId(id);
        positionEntity.setName(name);
        try {
            positionEntityMapper.updatePosition(positionEntity);
            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.SUCCESS.getCode(),
                    SystemConfig.ResponseCode.SUCCESS.getDesc(), "更新成功");
        }
        catch (Exception ex){
            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.ERROR.getCode(),
                    SystemConfig.ResponseCode.ERROR.getDesc(), ex.toString());
        }
    }

    /**
     * 删除岗位
     * @param id
     * @return
     */
    @Override
    public AjaxResponse deletePosition(String id){
        try {
            positionEntityMapper.deletePosition(id);
            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.SUCCESS.getCode(),
                    SystemConfig.ResponseCode.SUCCESS.getDesc(), "删除成功");
        }
        catch (Exception ex){
            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.ERROR.getCode(),
                    SystemConfig.ResponseCode.ERROR.getDesc(), ex.toString());
        }
    }

    /**
     * 删除用户的岗位
     * @param username
     * @return
     */
    @Override
    public AjaxResponse deletePositionUser(String username){
        try {
            positionEntityMapper.deletePositionUser(username);
            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.SUCCESS.getCode(),
                    SystemConfig.ResponseCode.SUCCESS.getDesc(), "删除成功");
        }
        catch (Exception ex){
            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.ERROR.getCode(),
                    SystemConfig.ResponseCode.ERROR.getDesc(), ex.toString());
        }
    }

    /**
     * 获取角色列表
     * @return
     */
    @Override
    public List<RolesEntity> rolesList(){
        return rolesEntityMapper.rolesList("");
    }

    /**
     * 获取角色列表分页
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public HashMap<String,Object> rolesListPage(String name,Integer page, Integer pageSize){
        HashMap<String,Object> hashMap=new HashMap<>();
        List<RolesEntity> rolesEntities=rolesEntityMapper.rolesList(name);
        if(pageSize==0){
            hashMap.put("list",rolesEntities);
            hashMap.put("total",rolesEntities.size());
        }
        else{
            Integer indexStart=(page-1)*pageSize;
            List<RolesEntity> rolesEntityList=rolesEntityMapper.rolesListPage(name,indexStart,pageSize);
            hashMap.put("list",rolesEntityList);
            hashMap.put("total",rolesEntities.size());
        }
        return  hashMap;
    }

    /**
     * 获取用户的角色
     * @param username
     * @return
     */
    @Override
    public List<RolesEntity> rolesListByUser(String username){
        return rolesEntityMapper.rolesListByUser(username);
    }

    /**
     * 增加角色
     * @param name
     * @return
     */
    @Override
    public AjaxResponse addRoles(String name){
        try {
            RolesEntity rolesEntity=new RolesEntity();
            rolesEntity.setId(UUID.randomUUID().toString());
            rolesEntity.setName(name);
            rolesEntityMapper.addRoles(rolesEntity);
            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.SUCCESS.getCode(),
                    SystemConfig.ResponseCode.SUCCESS.getDesc(), "添加成功");
        }
        catch (Exception ex){
            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.ERROR.getCode(),
                    SystemConfig.ResponseCode.ERROR.getDesc(), ex.toString());
        }
    }

    /**
     * 修改角色信息
     * @param id
     * @param name
     * @return
     */
    @Override
    public AjaxResponse updateRoles(String id,String name){
        try {
            RolesEntity rolesEntity=new RolesEntity();
            rolesEntity.setId(id);
            rolesEntity.setName(name);
            rolesEntityMapper.updateRoles(rolesEntity);
            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.SUCCESS.getCode(),
                    SystemConfig.ResponseCode.SUCCESS.getDesc(), "修改成功");
        }
        catch (Exception ex){
            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.ERROR.getCode(),
                    SystemConfig.ResponseCode.ERROR.getDesc(), ex.toString());
        }
    }

    /**
     * 删除角色
     * @param id
     * @return
     */
    @Override
    public AjaxResponse deleteRoles(String id){
        try {
            rolesEntityMapper.deleteRoles(id);
            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.SUCCESS.getCode(),
                    SystemConfig.ResponseCode.SUCCESS.getDesc(), "删除成功");
        }
        catch (Exception ex){
            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.ERROR.getCode(),
                    SystemConfig.ResponseCode.ERROR.getDesc(), ex.toString());
        }
    }

    /**
     * 删除用户的角色
     * @param username
     * @return
     */
    @Override
    public AjaxResponse deleteRoleUser(String username){
        try {
            rolesEntityMapper.deleteRoleUser(username);
            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.SUCCESS.getCode(),
                    SystemConfig.ResponseCode.SUCCESS.getDesc(), "删除成功");
        }
        catch (Exception ex){
            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.ERROR.getCode(),
                    SystemConfig.ResponseCode.ERROR.getDesc(), ex.toString());
        }
    }

    /**
     * 获取人员列表
     * @return
     */
    @Override
    public List<UserEntity> getUserEntities(){
        List<UserEntity> userEntityList = userEntityMapper.getUserEntityList("","","all");
        return userEntityList;
    }

    /**
     * 获取人员列表分页
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public HashMap<String,Object> getUserList(String name,String username,
                                              String departid,Integer page,Integer pageSize){
        HashMap<String,Object> hashMap=new HashMap<>();
        List<UserEntity> userEntityList = userEntityMapper.getUserEntityList(name,username,departid);
        if(pageSize==0){
            hashMap.put("list",userEntityList);
            hashMap.put("total",userEntityList.size());
        }
        else{
            Integer indexStart=(page-1)*pageSize;
            List<UserEntity> userEntities = userEntityMapper.getUserList(name,username,departid,indexStart,pageSize);
            hashMap.put("list",userEntities);
            hashMap.put("total",userEntityList.size());
        }
        return hashMap;
    }

    /**
     * 获取人员详细信息
     * @param username
     * @return
     */
    @Override
    public HashMap<String,Object> getUserEntity(String username){
        HashMap<String,Object> hashMap=new HashMap<>();
        try {
            UserEntity userEntityInfo = userEntityMapper.getUserEntityInfo(username);
            List<PositionEntity> positionEntities=positionEntityMapper.positionListByUser(username);
            List<RolesEntity> rolesEntities=rolesEntityMapper.rolesListByUser(username);
            hashMap.put("user",userEntityInfo);
            hashMap.put("positions",positionEntities);
            hashMap.put("roles",rolesEntities);
            return hashMap;
        }
        catch (Exception ex){
            return hashMap;
        }
    }

    /**
     * 增加人员
     * @param param
     * @return
     */
    @Override
    public AjaxResponse addUser(UserParams param){
        try {
            String password=passwordEncoder.encode("1");
            UserEntity userEntity=new UserEntity();
            userEntity.setName(param.getName());
            userEntity.setUsername(param.getUsername());
            userEntity.setPassword(password);
            userEntity.setTel(param.getTel());
            userEntity.setEmail(param.getEmail());
            userEntity.setSex(param.getSex());
            userEntity.setState(1);
            userEntity.setDepartid(param.getDepartid());
            userEntity.setRoles("ROLE_ACTIVITI_USER");
            userEntity.setGuuid(UUID.randomUUID().toString());

            userEntityMapper.addUser(userEntity);

            //修改岗位信息
            if(param.getPositions().length>0){
                positionEntityMapper.deletePositionUser(param.getUsername());
                for(String position: param.getPositions()){
                    userEntityMapper.addUserPosition(UUID.randomUUID().toString(),param.getUsername(),position);
                }
            }
            //修改角色信息
            if(param.getRoles().length>0){
                rolesEntityMapper.deleteRoleUser(param.getUsername());
                for(String role: param.getRoles()){
                    userEntityMapper.addUserRoles(UUID.randomUUID().toString(),param.getUsername(),role);
                }
            }

            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.SUCCESS.getCode(),
                    SystemConfig.ResponseCode.SUCCESS.getDesc(), "新增成功");
        }
        catch (Exception ex){
            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.ERROR.getCode(),
                    "新增失败", ex.toString());
        }
    }

    /**
     * 根据部门编号查找人员
     * @param departid
     * @return
     */
    @Override
    public AjaxResponse getUserByDepartId(Integer departid){
        try {
            List<UserEntity> userEntityList=userEntityMapper.getUserByDepartId(departid);
            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.SUCCESS.getCode(),
                    SystemConfig.ResponseCode.SUCCESS.getDesc(), userEntityList);
        }
        catch (Exception ex){
            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.ERROR.getCode(),
                    "修改失败", ex.toString());
        }
    }

    /**
     * 修改岗位信息
     * @param param
     * @return
     */
    @Override
    public AjaxResponse updateUser(UserParams param){
        try {
            UserEntity userEntity=new UserEntity();
            userEntity.setName(param.getName());
            userEntity.setUsername(param.getUsername());
            userEntity.setId(param.getId().longValue());
            userEntity.setTel(param.getTel());
            userEntity.setEmail(param.getEmail());
            userEntity.setSex(param.getSex());
            userEntity.setState(param.getState());
            userEntity.setDepartid(param.getDepartid());

            if(SystemConfig.IsNullOrEmpty(param.getGuuid())){
                userEntity.setGuuid(UUID.randomUUID().toString());
                userEntityMapper.addDeptUser(userEntity);
            }
            else{
                userEntity.setGuuid(param.getGuuid());
            }
            userEntityMapper.updateUser(userEntity);

            //修改岗位信息
            if(!SystemConfig.IsNullOrEmpty(param.getPositions())) {
                if (param.getPositions().length > 0) {
                    positionEntityMapper.deletePositionUser(param.getUsername());
                    for (String position : param.getPositions()) {
                        userEntityMapper.addUserPosition(UUID.randomUUID().toString(), param.getUsername(), position);
                    }
                }
            }
            //修改角色信息
            if(!SystemConfig.IsNullOrEmpty(param.getRoles())) {
                if (param.getRoles().length > 0) {
                    rolesEntityMapper.deleteRoleUser(param.getUsername());
                    for (String role : param.getRoles()) {
                        userEntityMapper.addUserRoles(UUID.randomUUID().toString(), param.getUsername(), role);
                    }
                }
            }
            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.SUCCESS.getCode(),
                    SystemConfig.ResponseCode.SUCCESS.getDesc(), "修改成功");
        }
        catch (Exception ex){
            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.ERROR.getCode(),
                    "修改失败", ex.toString());
        }
    }
    @Override
    public AjaxResponse updatePwd(String username,String oldPwd,String newPwd){
        try {
            UserInfo userInfo = mapper.getUserInfo(username);
            BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
            if(encoder.matches(oldPwd,userInfo.getPassword())) {
                String newPassword=passwordEncoder.encode(newPwd);
                userEntityMapper.updatePwd(username,newPassword);
            }
            else{
                return AjaxResponse.AjaxData(SystemConfig.ResponseCode.ERROR.getCode(),
                        "修改失败", "原密码不正确。");
            }
            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.SUCCESS.getCode(),
                    SystemConfig.ResponseCode.SUCCESS.getDesc(), "修改成功");
        }
        catch (Exception ex){
            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.ERROR.getCode(),
                    "修改失败", ex.toString());
        }
    }
    @Override
    public AjaxResponse resetPwd(String username){
        try {
            String newPassword=passwordEncoder.encode("1");
            userEntityMapper.updatePwd(username,newPassword);
            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.SUCCESS.getCode(),
                    SystemConfig.ResponseCode.SUCCESS.getDesc(), "修改成功");
        }
        catch (Exception ex){
            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.ERROR.getCode(),
                    "修改失败", ex.toString());
        }
    }
    @Override
    public AjaxResponse deleteUser(Integer id){
        try {
            UserEntity userEntity=new UserEntity();
            userEntity.setId(id.longValue());

            userEntityMapper.deleteUser(userEntity);
            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.SUCCESS.getCode(),
                    SystemConfig.ResponseCode.SUCCESS.getDesc(), "删除成功");
        }
        catch (Exception ex){
            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.ERROR.getCode(),
                    "删除失败", ex.toString());
        }
    }
}
